package com.ymatou.iapi.trading.sellerquery2.parameter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.ymatou.query.seller.trading.api.parameter.TimeRange;
import com.ymttest.common.enums.DateTypeEnum;
import com.ymttest.common.enums.LogisticsTypeEnum;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.YMTDateUtil;

public class Common {
	public static final int dayOfWeek = Calendar.getInstance().get(
			Calendar.DAY_OF_WEEK);
	public static final int dayOfMonth = Calendar.getInstance().get(
			Calendar.DAY_OF_MONTH);
	public static final int monthOfYear = Calendar.getInstance().get(
			Calendar.MONTH) + 1;
	public static final int dayOfYear = Calendar.getInstance().get(
			Calendar.DAY_OF_YEAR);
	
	public static int GetOrderStatusForSeller(int originalStatus, int rcStatus,
			Boolean forSeller) {
		if (forSeller == null) {
			forSeller = true;
		}
		if (!forSeller)
			return originalStatus;

		if (originalStatus != OrderStatusEnum.AccountPaid.getValue())
			return originalStatus;

		return rcStatus == 1 ? OrderStatusEnum.OrderEstablish.getValue()
				: OrderStatusEnum.AccountPaid.getValue();
	}

	public static List<Integer> OrderAvailiableStatus() {
		List<Integer> orderAvailiableStatus = new ArrayList<Integer>();
		orderAvailiableStatus.add(OrderStatusEnum.AccountPaid.getValue());
		orderAvailiableStatus.add(OrderStatusEnum.Shipped.getValue());
		orderAvailiableStatus.add(OrderStatusEnum.Received.getValue());
		orderAvailiableStatus.add(OrderStatusEnum.SellerAccept.getValue());
		orderAvailiableStatus.add(OrderStatusEnum.ApplyForPostPay.getValue());
		orderAvailiableStatus.add(OrderStatusEnum.OrderEstablish.getValue());
		return orderAvailiableStatus;
	}
	
	public static List<Integer> OrderAvailiableStatusWithoutOrderEstablish(){
		List<Integer> orderAvailiableStatusWithoutOrderEstablish = new ArrayList<Integer>();
		orderAvailiableStatusWithoutOrderEstablish.add(OrderStatusEnum.AccountPaid.getValue());
		orderAvailiableStatusWithoutOrderEstablish.add(OrderStatusEnum.Shipped.getValue());
		orderAvailiableStatusWithoutOrderEstablish.add(OrderStatusEnum.Received.getValue());
		orderAvailiableStatusWithoutOrderEstablish.add(OrderStatusEnum.SellerAccept.getValue());
		orderAvailiableStatusWithoutOrderEstablish.add(OrderStatusEnum.ApplyForPostPay.getValue());
		return orderAvailiableStatusWithoutOrderEstablish;
	}

	public static Boolean paidInFull = null;

	public static List<Integer> GetSellerOrderStatus(
			List<Integer> originalOrderStatus, boolean autoFillIfEmpty) {
		paidInFull = null;
		List<Integer> status = null;
		if (null == originalOrderStatus || originalOrderStatus.size() == 0) {
			if (autoFillIfEmpty) {
				status = Common.OrderAvailiableStatus();
				status.add(OrderStatusEnum.OrderPostPay.getValue());
			} else {
				return originalOrderStatus;
			}
		} else {
			status = originalOrderStatus;
		}
		if (status.contains(OrderStatusEnum.SellerAccept.getValue())
				&& !status.contains(OrderStatusEnum.OrderPostPay.getValue()))
		// 只要补款完成不要等待发起补款
		{
			status.removeIf(a -> a == OrderStatusEnum.SellerAccept.getValue());
			paidInFull = true;
		} else if (!status.contains(OrderStatusEnum.SellerAccept.getValue())
				&& status.contains(OrderStatusEnum.OrderPostPay.getValue()))
		// 只要等待发起补款不要补款完成
		{
			status.removeIf(a -> a == OrderStatusEnum.OrderPostPay.getValue());
			paidInFull = false;
		}
		return status;
	}

	public static Boolean GetPackageDeliveryDomesticDeliveredStatus(
			int orderStatus, int logisticsType, Boolean domesticDelivered) {
		if (logisticsType != LogisticsTypeEnum.PackageDelivery.getValue())
			return null;

		return DataUtil.Booleann.GetValueOrDefault(domesticDelivered);
	}

	public static TimeRange ToTimeRange(Integer type) {
		if (type == DateTypeEnum.All.getValue())
			return null;

		String today = YMTDateUtil.getSysTime();
		String begin = null;

		if (type == DateTypeEnum.Today.getValue()) {
			return new TimeRange(today,
					YMTDateUtil.getBeforeOrNextDay(today, 1));
		}
		if (type == DateTypeEnum.Yesterday.getValue()) {
			return new TimeRange(YMTDateUtil.getBeforeOrNextDay(today, -1),
					today);
		}
		if (type == DateTypeEnum.ThisWeek.getValue()) {
			int i = dayOfWeek == 1 ? 6 : dayOfWeek-2;
//			return new TimeRange(YMTDateUtil.getBeforeOrNextDay(today, 1 - i+1),
//					YMTDateUtil.getBeforeOrNextDay(today, 8 - i+1));
			return new TimeRange(YMTDateUtil.getBeforeOrNextDay(-1*i),
					YMTDateUtil.getBeforeOrNextDay(7-i));
		}
		if (type == DateTypeEnum.ThisMonth.getValue()) {
			begin = YMTDateUtil.getBeforeOrNextDay(today, 1 - dayOfMonth);
			return new TimeRange(begin, YMTDateUtil.getBeforeOrNextMonth(begin,
					1));
		}
		if (type == DateTypeEnum.ThisQuarter.getValue()) {
			String jan = YMTDateUtil
					.getBeforeOrNextMonth(0 - (monthOfYear - 1) % 3);
			begin = YMTDateUtil.getBeforeOrNextDay(jan, 1 - dayOfMonth);
			return new TimeRange(begin, YMTDateUtil.getBeforeOrNextMonth(begin,
					3));
		}
		if (type == DateTypeEnum.ThisYear.getValue()) {
			begin = YMTDateUtil.getBeforeOrNextDay(today, 1 - dayOfYear);
			return new TimeRange(begin, YMTDateUtil.getBeforeOrNextYear(begin,
					1));
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(dayOfWeek);
		String today = YMTDateUtil.getSysTime();
		System.out.println(YMTDateUtil.getBeforeOrNextDay(today, 1 - dayOfWeek));
		System.out.println(YMTDateUtil.getBeforeOrNextDay(today, 8 - dayOfWeek));
		int day = DateTypeEnum.ThisMonth.getValue();
		System.out.println(ToTimeRange(day).getBegin());
		System.out.println(ToTimeRange(day).getEnd());
	}
}
